Answer:

getContentPane().setBackground( Color.blue )

Complete Program

The following is a complete program, suitable for copying to a file and running.

import java.awt.*; 
import java.awt.event.*;
import javax.swing.*;

class ButtonFrame extends JFrame implements ActionListener
{
  JButton bChange ; // reference to the button object

  // constructor for ButtonFrame
  ButtonFrame() 
  {
    // construct a Button
    bChange = new JButton("Click Me!"); 
    
    // register the ButtonFrame object as the listener for the JButton.
    bChange.addActionListener( this ); 

    // add the button to the JFrame
    getContentPane().setLayout( new FlowLayout() );     
    getContentPane().add( bChange );      
    setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );   
  }
  
  public void actionPerformed( ActionEvent evt)
  {
    getContentPane().setBackground( Color.blue );
    repaint();  // ask the system to paint the screen.
  }

}

public class ButtonDemo
{
  public static void main ( String[] args )
  {
    ButtonFrame frm = new ButtonFrame();

    frm.setSize( 200, 150 );     
    frm.setVisible( true );   
  }
}

The repaint() method called in actionPerformed()tells the system to repaint the screen sometime soon because we have changed something. The system will do this when it is ready. Don't call paint().

QUESTION 17:

Should the entire frame be repainted every time a button is clicked?